/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include "transition.h"

Transition::Transition(IrrlichtDevice* device)
  {
    Device = device;
    IGUIEnvironment* guiEnv = Device->getGUIEnvironment();
    IImage* ishot = Device->getVideoDriver()->createScreenShot();
    Tshot = Device->getVideoDriver()->addTexture("screenshot", ishot);
    //Create the fader object
    Fader = guiEnv->addImage(Tshot, position2d<s32>(0, 0));
    Fader->setScaleImage(true);
    Fader->setAlignment(EGUIA_LOWERRIGHT,EGUIA_UPPERLEFT,EGUIA_LOWERRIGHT,EGUIA_UPPERLEFT);
    //Create a parent for the fader
    FaderPrt = guiEnv->addImage(Tshot,position2d<s32>(0, 0));
    FaderPrt->setColor(SColor(0,0,0,0));
    FaderPrt->addChild(Fader);
    FaderPrt->setVisible(false);
  }

Transition::~Transition()
  {
    //Fader->drop();
  }

void Transition::start(ETRANSITION_TYPE effect, int time, float factor)
{
  IsRunning = true;

  Effect = effect;
  Time = time;
  Factor = factor;
  StartTime = Device->getTimer()->getTime();
  EndTime = StartTime+ time;

  //Initialize
  IImage* ishot = Device->getVideoDriver()->createScreenShot();
  Tshot = Device->getVideoDriver()->addTexture("screenshot", ishot);
  FaderPrt->setVisible(true);
  Fader->setImage(Tshot);
  ishot->drop();

  InitSize = Device->getVideoDriver()->getScreenSize();
  EndSize.Height = (s32)(InitSize.Height * Factor);
  EndSize.Width = (s32)(InitSize.Width * Factor);

  //Re-Set
    FaderPrt->setRelativePosition(rect<s32>(position2d<s32>(0,0),InitSize));
    FaderPrt->setColor(SColor(0,255,255,255));
    Fader->setRelativePosition(rect<s32>(position2d<s32>(0,0),InitSize));
    Fader->setColor(SColor(255,255,255,255));
}

void Transition::draw()
{
  FaderPrt->getParent()->bringToFront(FaderPrt);
  Fader->getParent()->bringToFront(Fader);

  position2d<s32> position(0,0);

  u32 now = Device->getTimer()->getTime();
  if (now <= EndTime)
    {
      f32 f = (EndTime - now) / (f32)(EndTime - StartTime);
      SColor color(255, 255, 255, 255);
      SColor newcolor = color.getInterpolated(SColor(0,255,255,255),f);
      switch (Effect)
        {
      case ETT_FADE:
        Fader->setScaleImage(true);
        Size.Height = EndSize.Height - (EndSize.Height - InitSize.Height) *  f;
        Size.Width = EndSize.Width - (EndSize.Width - InitSize.Width) * f;
        position.X =   (InitSize.Width - Size.Width) / 2;
        position.Y = (InitSize.Height - Size.Height) / 2;
        Fader->setRelativePosition(rect<s32> (position,Size));
        Fader->setColor(newcolor);
        break;
      case ETT_WIPE:
        Fader->setScaleImage(false);
        //UP
        if (Factor == 1)
          {
            Size.Height = InitSize.Height * f;
            Size.Width = InitSize.Width;
            Fader->setRelativePosition(rect<s32> (position2d<s32> (0, 0), Size));
          }
        //RIGHT
        if (Factor == 2)
          {
            Size.Height = InitSize.Height;
            Size.Width = InitSize.Width * f;
            position.X = InitSize.Width - Size.Width;
            position.Y = 0;
            FaderPrt->setRelativePosition(rect<s32> (position, Size));
          }
        //DOWN
        if (Factor == 3)
          {
            Size.Height = InitSize.Height * f;
            Size.Width = InitSize.Width;
            position.X = 0;
            position.Y = InitSize.Height - Size.Height;
            FaderPrt->setRelativePosition(rect<s32> (position, Size));
          }
        //LEFT
        if (Factor == 4)
          {
            Size.Height = InitSize.Height;
            Size.Width = InitSize.Width * f;
            Fader->setRelativePosition(rect<s32> (position2d<s32> (0, 0), Size));
          }
        break;
      case ETT_ROTATE:
        FaderPrt->setScaleImage(false);
        if(Factor == 1)
          {
            Fader->setRelativePosition(rect<s32>(position2d<s32>(0,-InitSize.Height + (InitSize.Height * f)),InitSize));
          }
        if(Factor == 2)
          {
            Fader->setRelativePosition(rect<s32>(position2d<s32>(InitSize.Width - (InitSize.Width * f),0),InitSize));
          }
        if(Factor == 3)
          {
            Fader->setRelativePosition(rect<s32>(position2d<s32>(0,InitSize.Height - (InitSize.Height * f)),InitSize));
          }
        if(Factor == 4)
          {
            Fader->setRelativePosition(rect<s32>(position2d<s32>(-InitSize.Width + (InitSize.Width * f),0),InitSize));
          }
        break;
        }
    }
  else
    {
      //Reset!
      FaderPrt->setVisible(false);
      Fader->setImage(0);
      //Tshot->drop();
      IsRunning = false;

      //Re-Set
        FaderPrt->setRelativePosition(rect<s32>(position2d<s32>(0,0),InitSize));
        FaderPrt->setColor(SColor(0,255,255,255));
        Fader->setRelativePosition(rect<s32>(position2d<s32>(0,0),InitSize));
        Fader->setColor(SColor(255,255,255,255));
    }
}


